<?php
/*
 * Created on Feb 17, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

class UserInfoModel{	
	
	function UserInfoModel(){
	
	}

	function getCountUser(){		
		global $connDB;  //The database connection
		$sql = "SELECT * FROM ".TBL_USERS." WHERE deleteFlag = 0";
		$result = $connDB->DB_query($sql,$connDB->connection);
      	return $connDB->DB_num_rows($result);
	}
	
	function getUsers($offset = null, $limit = null){
		global $connDB;  //The database connection
		$sql = "SELECT * FROM ".TBL_USERS." WHERE deleteFlag = 0 ".getLimitFormat($offset, $limit);
		$result = $connDB->DB_query($sql,$connDB->connection);
      	return $result;
	}
	
	function getArrayUsers($resultSet = null){
		global $connDB;  //The database connection
		if($resultSet == null) return null;
		return $connDB->DB_fetch_row($resultSet);
	}
	
	function getUserById($id = null){
		global $connDB;  //The database connection
		if($id == null) return null;
		$sql = "SELECT userName,
						password,
						fullName,
						phone,
						place,
						email,
						statusFlag,
						createDate
			FROM ".TBL_USERS." 
			WHERE id = '$id' AND deleteFlag = 0";
		$result = $connDB->DB_query($sql,$connDB->connection);
		return $connDB->DB_fetch_array($result);;
	}
	
	function getUserByUsername($username){
   		global $connDB;  //The database connection
      	$sql = "SELECT * FROM ".TBL_USERS." WHERE userName = '$username'";
      	$result = $connDB->DB_query($sql,$connDB->connection);
      	/* Error occurred, return given name by default */
      	if(!$result || ($connDB->DB_num_rows($result) < 1)){
         	return NULL;
      	}
      	/* Return result array */
      	$dbarray = $connDB->DB_fetch_array($result);
      	return $dbarray;
   	}
	
	/**
    * usernameTaken - Returns true if the username has
    * been taken by another user, false otherwise.
    */
    function userNameTaken($username){
		global $connDB;  //The database connection
		if(!get_magic_quotes_gpc()){
			$username = addslashes($username);
		}
		$sql = "SELECT userName FROM ".TBL_USERS." WHERE userName = '$username'";
		$result = $connDB->DB_query($sql,$connDB->connection);
		return ($connDB->DB_num_rows($result) > 0);
    }
   
	/**
    * updateUserField - Updates a field, specified by the field
    * parameter, in the user's row of the database.
    */
   function updateUserField($username, $field, $value){
   	global $connDB;  //The database connection
      $sql = "UPDATE ".TBL_USERS." SET ".$field." = '$value' WHERE userName = '$username'";
      return $connDB->DB_query($sql,$connDB->connection);
   }
   
	function login($username, $password, $level){
		global $connDB;  //The database connection
		/* Add slashes if necessary (for query) */
      	if(!get_magic_quotes_gpc()) {
	      	$username = addslashes($username);
      	}

      	/* Verify that user is in database */
      	$sql = "SELECT password FROM ".TBL_USERS." WHERE deleteFlag = 0 AND userName = '$username' AND userlevel = $level";
      	$result = $connDB->DB_query($sql,$connDB->connection);
      	if(!$result || ($connDB->DB_num_rows($result) < 1)){
         	return 1; //Indicates username failure
      	}

      	/* Retrieve password from result, strip slashes */
      	$dbarray = $connDB->DB_fetch_array($result);
      	$dbarray['password'] = stripslashes($dbarray['password']);
      	$password = stripslashes($password);

      	/* Validate that password is correct */
      	if($password == $dbarray['password']){
        	return 0; //Success! Username and password confirmed
      	}
      	else{
         	return 2; //Indicates password failure
      	}
	}
	
	function addNewUser($subuser, $subpass, $subemail, $subFullName, $subPlace, $subPhone, $status, $createDate){
		global $connDB;  //The database connection
		$createDate = FormatDateForSQL($createDate).' '.date('h:i:s');
		$sql = "INSERT INTO ".TBL_USERS." (userName,
						password,
						fullName,
						phone,
						place,
						email,
						statusFlag,
						deleteFlag,
						createDate,
						activeDate,
						updateDate,
						lastLoginDate)
				VALUES ('" . $subuser . "',
					'" . $subpass . "',
					'" . $subFullName . "',
					'" . $subPhone . "',
					'" . $subPlace . "',
					'" . $subemail . "',
					" . $status . ",
					" . 0 . ", 
					'". $createDate ."',
					'". $createDate ."',
					'". $createDate ."',
					'". $createDate ."'			
					)";
		
		$msg = _('Tài khỏan <') . ' ' . $subFullName . ' ' . _('> đã thêm vào hệ thống');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function editUser($userId, $subpass, $subemail, $subFullName, $subPlace, $subPhone, $status){
		global $connDB;  //The database connection
		$updatePass = '';
		if($subpass != null)
			$updatePass = "password='" . $subpass . "',";
		else $updatePass = "password=password,";
		$sql = "UPDATE ".TBL_USERS." SET fullName='" . $subFullName . "',
						phone='" . $subPhone . "',
						place='" . $subPlace . "',
						email='" . $subemail . "',".
						$updatePass.
						"statusFlag=" . $status . ",
						updateDate='" . date('Y-m-d h:i:s') . "' 
				WHERE id = '$userId'";
		$msg = _('Khu đô thị <') . ' ' . $subFullName . ' ' . _('> đã cập nhật');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
	
	function deleteUser($userId){
		global $connDB;  //The database connection
		$sql = "UPDATE ".TBL_USERS." SET deleteFlag=" . 1 . ",
						updateDate='" . date('Y-m-d h:i:s') . "'
				WHERE id = '$userId'";			
		$msg = _('Khu đô thị <') . ' ' . $userId . ' ' . _('> xóa');
		$ErrMsg = _('Thao tác trên database bị lỗi xảy ra');
		$DbgMsg = _('Lệnh cập nhật database bị lỗi');
		$result = $connDB->DB_query($sql, $connDB->connection, $ErrMsg, $DbgMsg);
		if($result){
			return 0;
		}else{
			return 2;
		}
	}
};

/* Create userinfo controller object */
$userModel = new UserInfoModel;
?>